package com.ws.dao.impl;

import com.ws.dao.BikeDao;
import com.ws.entity.BaseBike;
import com.ws.entity.BikeCompany;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Comparator;

/**
 * @author 王硕
 * @version jdk21
 * @className BikeDaoImpl
 * @description
 * @date 2024/11/16 15:01
 */
public class BikeDaoImpl implements BikeDao {

    @Override
    public void viewBikeCompany(BikeCompany[] bikeCompanies) {
        for (BikeCompany company : bikeCompanies) {
            System.out.println("公司序号 | 公司名称 | 公司单车数量 | 公司单车借出次数");
            System.out.println("+--------+----------+--------------+---------------------+");
            if (company != null) {
                System.out.format("%-10d | %-10s | %-15d | %-20d\n",
                        company.getCompanyId(),
                        company.getCompanyName(),
                        company.getSum(),
                        company.getCount());
                System.out.println("+--------+----------+--------------+---------------------+");

                // 调用viewBike方法输出单车信息
                viewBike(company.getBikes());
            }
        }
    }

    @Override
    public void viewBike(BaseBike[] bikes) {
        if (bikes != null) {
            System.out.println("单车序号 | 单车品牌 | 单车状态 | 借出时间 | 归还时间");
            System.out.println("+--------+----------+----------+----------+----------+");
            for (BaseBike bike : bikes) {
                if (bike != null) {
                    String statusDesc = bike.getStatus() == 1 ? "可借" : "已借";
                    String borrowTimeStr = bike.getBorrowTime() != null ? bike.getFormattedBorrowTime() : "未借出";
                    System.out.format("%-10d | %-10s | %-10s | %-10s | %-10s\n",
                            bike.getBid(),
                            bike.getBname(),
                            statusDesc,
                            borrowTimeStr,
                            bike.getReturnTime() != null ? bike.getReturnTime().toString() : "未归还");
                }
            }
            System.out.println("+--------+----------+----------+----------+----------+");
        }
    }
    @Override
    public void dropBike(BikeCompany company, int num) {

        BaseBike[] bikes = company.getBikes();
        int sum = company.getSum();
        int leftSpace = bikes.length - sum;
        if (leftSpace < num){
            bikes = Arrays.copyOf(bikes, (sum + num) * 2);
        }

        for (int i = 1; i <= num; i++) {
            BaseBike lastBikeObj = bikes[sum - 1];
            bikes[sum++] = lastBikeObj.clone();
        }
        company.setBikes(bikes);
        company.setSum(company.getSum() + num);
        System.out.println("投放后 " + company.getCompanyName() + " 的单车信息：");
        viewBike(company.getBikes());
        System.out.println("成功投放 " + num + " 辆单车到 " + company.getCompanyName());
    }

    @Override
    public void deleteBike(BikeCompany company, Integer bid) {
        if (company == null) {
            System.out.println("未找到对应公司，删除失败");
            return;
        }
        for (int i = 0; i < company.getBikes().length; i++) {
            BaseBike bike = company.getBikes()[i];
            if (bike != null && bike.getBid().equals(bid) && bike.getStatus() == 1) {
                BaseBike[] newBikesArray = new BaseBike[company.getBikes().length - 1];
                System.arraycopy(company.getBikes(), 0, newBikesArray, 0, i);
                System.arraycopy(company.getBikes(), i + 1, newBikesArray, i, company.getBikes().length - i - 1);
                company.setBikes(newBikesArray);
                company.setSum(company.getSum() - 1);
                System.out.println("删单车"+bike.getBid()+"成功");
                return;
            }
        }
        System.out.println("未找到指定的单车或单车正在被借出，无法删除。");
    }

    @Override
    public void loanBike(BikeCompany company, Integer bid) {
        if (company == null) {
            System.out.println("未找到对应公司，借出失败");
            return;
        }
        for (int i = 0; i < company.getBikes().length; i++) {
            BaseBike bike = company.getBikes()[i];
            if (bike != null && bike.getBid().equals(bid) && bike.getStatus() == 1) {
                bike.setStatus(2);
                bike.setBorrowTime(LocalDateTime.now());
                company.setCount(company.getCount()+1);
                System.out.println("单车" + bid + "借出成功，借出时间为：" + bike.getFormattedBorrowTime());
                return;
            }
        }
        System.out.println("未找到指定的单车或单车正在被借出，无法借出。");
    }

    @Override
    public void returnBike(BikeCompany company, Integer bid) {
        if (company == null) {
            System.out.println("未找到对应公司，归还失败");
            return;
        }
        for (int i = 0; i < company.getBikes().length; i++) {
            BaseBike bike = company.getBikes()[i];
            if (bike != null && bike.getBid().equals(bid) && bike.getStatus() == 2) {
                bike.setStatus(1);
                bike.setReturnTime(LocalDateTime.now());
                long seconds = java.time.Duration.between(bike.getBorrowTime(), bike.getReturnTime()).getSeconds();
                double price = seconds;
                System.out.println("单车" + bid + "归还成功，归还时间为：" + bike.getFormattedReturnTime() + "，使用时间为" + seconds + "秒，费用为" + price + "元");
                return;
            }
        }
        System.out.println("未找到指定的单车或单车未被借出，无法归还");
    }

    @Override
    public void viewLoanRank(BikeCompany[] bikeCompanies) {
        if (bikeCompanies == null || bikeCompanies.length == 0) {
            System.out.println("没有公司数据，无法显示排行榜。");
            return;
        }
        BikeCompany[] companies = Arrays.copyOf(bikeCompanies, bikeCompanies.length);
        Arrays.sort(companies, Comparator.comparingInt((BikeCompany company) -> company.getCount()));
        System.out.println("公司借出排行榜：");
        System.out.println("+------+----------+--------------+");
        for (BikeCompany company : companies) {
            System.out.format("%-5d | %-10s | %-15d\n", company.getCompanyId(), company.getCompanyName(), company.getCount());
        }
        System.out.println("+------+----------+--------------+");
    }
}
